from flask import Flask , render_template , jsonify
from blueprint.weather_submit import bp as wsbp
from blueprint.home import bp as home_bp
from blueprint.blog import bp as blog_bp
from blueprint.tools import bp as tools_bp
from exts import blogDB
app = Flask(__name__)


# 配置文件
UPLOAD_FOLDER = 'static/uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


# 注册一级标题下面的所有内容
app.register_blueprint(home_bp)



@app.route('/api/blogs')
def get_blogs():
    # 查询包含'display'标签的博客，按创建时间降序排列
    blogs = blogDB.find({'tags': 'display'}).sort('created_at', -1)
    blog_list = []
    for blog in blogs:
        # 处理日期格式
        created_at = blog['created_at']['$date'] if isinstance(blog['created_at'], dict) else blog['created_at']
        # 生成摘要：取raw_content的前100个字符
        excerpt = blog['raw_content'][:100] + '...' if len(blog['raw_content']) > 100 else blog['raw_content']
        blog_data = {
            'title': blog['title'],
            'excerpt': excerpt,
            'slug': blog['slug'],
            'tags': [tag for tag in blog['tags'] if tag != 'display'],  # 过滤掉display标签
            'created_at': created_at,
            'image_url': blog.get('image_url', '默认图片URL')  # 确保有默认图片
        }
        blog_list.append(blog_data)
    return jsonify(blog_list)

@app.route('/weather')
def he2():
    return render_template('weather.html')

# 必需要在路由函数之前注册过滤器
# 自定义时间格式过滤器

@app.route('/test')
def index():

    return render_template('blog/editor.html')
@app.route('/test1')
def index1():

    return render_template('lin/weather.html')
app.register_blueprint(wsbp)
app.register_blueprint(blog_bp)

app.register_blueprint(tools_bp)

if __name__ == '__main__':
    app.run()
